package com.qf.sl.youshare.adapters;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

/**
 * Created by IDEA
 * User : sl
 * on 2015/2/4.
 * 17:24
 */

/**
 * 通用的图片显示 Adapter
 */
public class CommonImageAdapter extends PagerAdapter {

    private List<Integer> imageIds;

    private Context context;

    public CommonImageAdapter(List<Integer> imageIds, Context context) {
        this.imageIds = imageIds;
        this.context = context;
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (imageIds != null) {
            ret = imageIds.size();
        }
        return ret;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    //创建和销毁视图

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        ImageView ret = new ImageView(context);

        ret.setScaleType(ImageView.ScaleType.FIT_XY);

        ret.setImageResource(imageIds.get(position));

        container.addView(ret);

        return ret;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (object instanceof ImageView){
            container.removeView((View) object);
        }
    }
}
